# Maintainer: Ivan Gabaldon <aur[at]inetol.net>
# Contributor: Sonu Ishaq <sonuishaq67@gmail.com>

pkgname=deskreen
pkgver=2.0.4
pkgrel=7
pkgdesc='Turn any device into a secondary screen for your computer'
arch=('aarch64'
      'x86_64')
url='https://deskreen.com'
license=('AGPL-3.0-or-later')
makedepends=('nodejs-lts-iron'
             'yarn')
conflicts=("$pkgname-bin")
source=("$pkgname-$pkgver.tar.gz::https://github.com/pavlobu/$pkgname/archive/v$pkgver.tar.gz"
        "$pkgname.desktop")
noextract=("$pkgname-$pkgver.tar.gz")
b2sums=('bef83c836ac722a854b524bc829b8e2f5a395a10d8d5794a148f0ef7f7a9f1292f99e99f219b10a9d550986d224c744047288a62fa37dcc062bebdbe42684e65'
        'e06627d9d11e385cf1f779e550d7cf050fee92c81057270d4931546520a0bcf59d8dad0b80264468e307437743ca93aac2011ad3fa582cd04b8be22929b8489a')

prepare() {
    mkdir -p "$pkgname-$pkgver/"
    bsdtar -xpf "$pkgname-$pkgver.tar.gz" --strip-components=1 -C "$pkgname-$pkgver/"
}

build() {
    # "error:0308010C:digital envelope routines::unsupported" -> https://github.com/webpack/webpack/issues/14532
    export NODE_OPTIONS=--openssl-legacy-provider

    yarn set version stable

    cd "$pkgname-$pkgver/app/client/"
    yarn install
    yarn up typescript@4.x

    cd ../../
    yarn install
    yarn up typescript@4.x
    yarn build

    case "$CARCH" in
        'aarch64')
            yarn electron-builder build --arm64 -l dir
            mv release/linux-arm64-unpacked/ release/linux-unpacked/
            ;;
        'x86_64')
            yarn electron-builder build --x64 -l dir
            ;;
    esac

    cd ../
    cp "$pkgname-$pkgver/resources/icons/icon_512x512.png" "$pkgname-$pkgver/release/linux-unpacked/$pkgname.png"
    cp "$srcdir/$pkgname.desktop" "$pkgname-$pkgver/release/linux-unpacked/$pkgname.desktop"
}

package() {
    depends=('alsa-lib'
             'at-spi2-core'
             'cairo'
             'dbus'
             'expat'
             'gcc-libs'
             'gdk-pixbuf2'
             'glib2'
             'glibc'
             'gtk3'
             'hicolor-icon-theme'
             'libcups'
             'libdrm'
             'libx11'
             'libxcb'
             'libxcomposite'
             'libxdamage'
             'libxext'
             'libxfixes'
             'libxkbcommon'
             'libxrandr'
             'mesa'
             'nspr'
             'nss'
             'pango')

    install -d "$pkgdir/opt/$pkgname/"
    cp -a "$pkgname-$pkgver/release/linux-unpacked/." "$pkgdir/opt/$pkgname/"

    chmod 755 "$pkgdir/opt/$pkgname/$pkgname"

    install -d "$pkgdir/usr/bin/"
    ln -s "/opt/$pkgname/$pkgname" "$pkgdir/usr/bin/$pkgname"

    install -d "$pkgdir/usr/share/applications/"
    ln -s "/opt/$pkgname/$pkgname.desktop" "$pkgdir/usr/share/applications/$pkgname.desktop"

    install -d "$pkgdir/usr/share/icons/hicolor/512x512/apps/"
    ln -s "/opt/$pkgname/$pkgname.png" "$pkgdir/usr/share/icons/hicolor/512x512/apps/$pkgname.png"
}
